#include "Include.h"

vu8  ADIGBT,AD_Vac, AD_Iac, Temp_IGBT, Temp_Coil;
vu16 ADCUR, Temp_PAN,ADVOL;
vu8 Adrl_Buf,Adrh_Buf,Ad_ChannelIndex,Ad_value, ADPAN;
u8 AD_Iac_Buf[8+1]={0};
u8 AD_Vac_Buf[4+1]={0};
u8 Moving_AVG_Filter(u8 Input_Data, u8 * p_filter_buf, u8 Length);



void ADC_Init(void)//2CH mode Init
{
	Tcr_buf = 65535-(4*625);//625us
	
    _sadc1 = 0b01001011;//opout   Fadck=Fsys/8=16/8=2Mhz
	_sadc0 = 0b00101111; //ADC ENABLE
	
	_atac1c = 0b00100001;
	_atac2c = 0b00010001;
	_atace = 1;//ENABLE ADC 2CH Mode
	
	_sadc2 = 0b10000101;//A/D conversion N=16
	
	_tcrc = 0b00000000;//fh/4
	_tcrl = Tcr_buf;
	_tcrh = (Tcr_buf >> 8);
	
	_atadt = 0b00000000;//0.5us The delay time between the completion of CH1 conversion and the start of CH2 conversion.

	_sado1bl=0;	 _sado1bh=0;
	_sado2bl=0;	 _sado2bh=0;

	_ade = 1;//
//	
	_atac1ss = 1;//CH1 External channel
	//ADCUR AN8
	_atac1s3 = 1;
	_atac1s2 = 0;
	_atac1s1 = 0;
	_atac1s0 = 0;	
	
	_aadcst=1; 
	
    bAD_DataReady = 0;
    bAD_StartCapture = 0;
}



void ADC_Task()
{
    static vu16 v_buf_ch1,v_buf_ch2=0;
    
    if (1 == bAD_Convert_Ok) //  A piece of data OK
    {
		bAD_Convert_Ok = 0;
    		
		v_buf_ch1 =  _sado1bh<< 8;
		v_buf_ch1 = v_buf_ch1+_sado1bl;		
		v_buf_ch1 = v_buf_ch1/16;
		v_buf_ch1 = v_buf_ch1>>4;

		//=============================		
		v_buf_ch2 = _sado2bh<< 8;
		v_buf_ch2 = v_buf_ch2+_sado2bl;			
		v_buf_ch2 = v_buf_ch2/16;
		v_buf_ch2 = v_buf_ch2>>4;

        if (Ad_ChannelIndex >= 5) // Number of channels
        {
            Ad_ChannelIndex = 0;
        }
    	switch (Ad_ChannelIndex) //
    	{
	    //--------------
	    case 0:		
			//2CH mode				
			_atac1ss = 1;//CH1 External channel
			//ADCUR AN8
			_atac1s3 = 1;
			_atac1s2 = 0;
			_atac1s1 = 0;
			_atac1s0 = 0;
			
			_atac2ss = 1;//CH2 External channel
			//ADVOL	AN1
			_atac2s3 = 0;
			_atac2s2 = 0;
			_atac2s1 = 0;
			_atac2s0 = 1;
	        break;
	   		//--------------
	   	case 1:	   	
	   		break;
	   		//--------------
	   	case 2:	   	
	   		break;
	   		//--------------
	   	case 3:	   	
	   		break;
	   		//--------------	
	    case 4:
			//2CH mode
			_atac1ss = 1;//CH1 External channel
			//ADPAN	AN4 
			_atac1s3 = 0;
			_atac1s2 = 1;
			_atac1s1 = 0;
			_atac1s0 = 0;

			_atac2ss = 1;//CH2 External channel
			//ADIGBT AN5 
			_atac2s3 = 0;
			_atac2s2 = 1;
			_atac2s1 = 0;
			_atac2s0 = 1;   
	        break;
			//--------------
	    default:
	        break;
	    }

        _aadcst=1; 
        	
        switch (Ad_value)//GET ad_value
        {
        case 0:  		 
     		 ADCUR = v_buf_ch1; //8bit
    		 ADVOL = v_buf_ch2;	//8bit
            break;
            //----------------------------
        case 1:
        	 ADCUR = ADCUR+v_buf_ch1; //8bit
     		 ADVOL = ADVOL+v_buf_ch2; //8bit
        	break;
        	//----------------------------
        case 2:
         	 ADCUR = ADCUR+v_buf_ch1; //8bit
     		 ADVOL = ADVOL+v_buf_ch2; //8bit               
        	break;
        	//----------------------------
        case 3:
        	 ADCUR = ADCUR+v_buf_ch1; //8bit
        	 ADCUR = ADCUR/4;
     		 ADVOL = ADVOL+v_buf_ch2; //8bit
     		 ADVOL = ADVOL/4;
        	break;
        	//----------------------------
        case 4:                  	         			         		
     		 ADPAN = v_buf_ch1;  //8BIT            		 
     		 ADIGBT = v_buf_ch2; //8bit            		
            break;
            //----------------------------
        default:
            break;
        }

        Ad_value++;
        if (Ad_value >= 5) // Number of channels
        {
            Ad_value=0;
            bAD_DataReady = 1;
			bAD_StartCapture = 1;			
        }
    }


    if (bAD_StartCapture == 1) // Copy ready ADC data
    {
    	
        bAD_StartCapture = 0;
		
        Temp_PAN = ADPAN;
        Temp_IGBT = ADIGBT;
         
        AD_Vac = ADVOL;
       // AD_Iac=ADCUR;
		 
		if(StartWorkCount> 25)  //Prevent misjudgment as shifting the pot
		{
			AD_Iac=Moving_AVG_Filter(ADCUR, AD_Iac_Buf, (8+1) )	;
	  		//AD_Vac=Moving_AVG_Filter(ADVOL, AD_Vac_Buf, (4+1) )	;		
			
		}
		else
		{
			_acc=Moving_AVG_Filter(ADCUR, AD_Iac_Buf, (8+1) )	;//
			AD_Iac=ADCUR	;
			//AD_Vac = ADVOL; 
		}
   }
}
/*******************************************************************************
** Name: Moving_AVG_Filter1
** Description: Data moving average: the high byte saved, low byte discarded 
** Input: Input_Data  / * p_filter_buf:array  / Length:Filter NUM+1
** Ouput: OutPut_Data  
********************************************************************************/
u8 Moving_AVG_Filter(u8 Input_Data, u8 * p_filter_buf, u8 Length) 
{
	vu8 i;
	vu8 OutPut_Data; 
	vu16 filter_sum=0 ;

	p_filter_buf[Length-1] = Input_Data;  //the high byte is new data
	filter_sum = 0;
 	for(i = 0; i < (Length-1); i++)  		//the high byte saved, low byte discarded 
	{
		p_filter_buf[i] = p_filter_buf[i + 1]; // 
		filter_sum = filter_sum +p_filter_buf[i];
	} 
 		OutPut_Data=filter_sum / (Length-1);   //OutPut_Data=filter_sum / FILTER_N; 
 		
	return (OutPut_Data);
}


